package org.example.netty;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class NettyServer {
    public static void main(String[] args) {
        Map<Channel, List<String>> mp = new ConcurrentHashMap<>();
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup());
        serverBootstrap.channel(NioServerSocketChannel.class);
        serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel socketChannel) throws Exception {
                socketChannel.pipeline().addLast(new LineBasedFrameDecoder(1024))
                        .addLast(new StringDecoder())
                        .addLast(new StringEncoder())
                        .addLast(new ResposeHandler())
                        .addLast(new DbHandler(mp));
            }
        });

        ChannelFuture bindFuture = serverBootstrap.bind(8080);
        bindFuture.addListener(f->{
            if(f.isSuccess()){
                System.out.println("服务器绑定端口成功");
            }else {
                System.out.println("服务器绑定端口失败");
            }
        });
    }
    static class ResposeHandler extends SimpleChannelInboundHandler<String>{
        @Override
        protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
            System.out.println(s);
            String msg = s + " world\n";
            channelHandlerContext.channel().writeAndFlush(msg);
            //将消息传到下一个handler
            channelHandlerContext.fireChannelRead(msg);
        }

        @Override
        public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
            System.out.println("channel注册了");
            //传给下一个handler的事件监听器
            ctx.fireChannelRegistered();
        }
    }

    static class DbHandler extends SimpleChannelInboundHandler<String> {
        private Map<Channel, List<String>> mp;

        public DbHandler(Map<Channel, List<String>> mp) {
            this.mp = mp;
        }

        @Override
        protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
            List<String> stringList = mp.computeIfAbsent(channelHandlerContext.channel(), k-> new ArrayList<>());
            stringList.add(s);
        }

        @Override
        public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
            System.out.println("channel注册了");
        }

        @Override
        public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
            System.out.println("channel解除注册了");
        }

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            System.out.println(ctx.channel() + " can use");
        }

        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            List<String> msgList = mp.get(ctx.channel());
            System.out.println(msgList);
        }
    }
}
